package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.excel.domain.OrderDetailSendEntity;
import com.ygqh.baby.handler.SendOrderDetailImportHandler;
import com.ygqh.baby.handler.SplitSendDetailHandler;
import com.ygqh.baby.model.OrderSendDetailSearchParam;
import com.ygqh.baby.model.OrderSendParam;
import com.ygqh.baby.model.SkuSendSplitRuleBySourceSku;
import com.ygqh.baby.model.SkuSendSplitRuleByTargetSku;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgOrderSendDetail;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.YgOrderSendDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/4/17 17:12
 * @since 1.0.0
 */
@RequestMapping("/admin/sendDetail")
@Controller
public class OrderSendDetailController extends BaseController {

    @Autowired
    private YgOrderSendDetailService ygOrderSendDetailService;

    @Autowired
    private FileService fileService;

    @Autowired
    private SendOrderDetailImportHandler sendOrderDetailImportHandler;

    @Autowired
    private SplitSendDetailHandler splitSendDetailHandler;


    @RequestMapping("setDetailSplitRule")
    @ResponseBody
    public Message setDetailSplitRule(@RequestBody SkuSendSplitRuleByTargetSku skuSendSplitRule) {
        AdminUser session = getSession();
        skuSendSplitRule.setHandler(session.getTrueName());
        skuSendSplitRule.setUpdateTime(new Date());
        return splitSendDetailHandler.setSplitRule(skuSendSplitRule);
    }

    @RequestMapping("deleteByTargetSku")
    @ResponseBody
    public Message deleteByTargetSku(String targetSkuCode) {
        return splitSendDetailHandler.deleteByTargetSku(targetSkuCode);
    }

    @RequestMapping("deleteBySourceSku")
    @ResponseBody
    public Message deleteBySourceSku(String sourceSkuCode) {
        return splitSendDetailHandler.deleteBySourceSku(sourceSkuCode);
    }

    @RequestMapping("getSplitRuleByTargetSku")
    @ResponseBody
    public Message getSplitRuleByTargetSku(String targetSkuCode) {
        SkuSendSplitRuleByTargetSku splitRuleByTargetSku = splitSendDetailHandler.getSplitRuleByTargetSku(targetSkuCode);
        return Message.success(splitRuleByTargetSku);
    }

    @RequestMapping("getSplitRuleListByTargetSku")
    @ResponseBody
    public Map<String, Object> getSplitRuleListByTargetSku(String targetSkuCode) {
        List<SkuSendSplitRuleByTargetSku> skuSendSplitRuleByTargetSkus = splitSendDetailHandler.getSplitRuleListByTargetSku(targetSkuCode);
        return new ResultSet<>(skuSendSplitRuleByTargetSkus.size(), skuSendSplitRuleByTargetSkus).toDatatables(null);
    }

    @RequestMapping("getSplitRuleListBySourceSku")
    @ResponseBody
    public Map<String, Object> getSplitRuleListBySourceSku(String sourceSkuCode) {
        List<SkuSendSplitRuleBySourceSku> splitRuleListBySourceSku = splitSendDetailHandler.getSplitRuleListBySourceSku(sourceSkuCode);
        return new ResultSet<>(splitRuleListBySourceSku.size(), splitRuleListBySourceSku).toDatatables(null);
    }

    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> search(OrderSendDetailSearchParam sendDetailSearchParam) {

        ResultSet<YgOrderSendDetail> search = ygOrderSendDetailService.search(sendDetailSearchParam);
        return search.toDatatables(null);
    }

    @RequestMapping("send")
    @ResponseBody
    public Message send(OrderSendParam sendParam) {
        return ygOrderSendDetailService.sendOrder(sendParam);
    }

    @RequestMapping("close")
    @ResponseBody
    public Message close(Long id, String remark) {
        AdminUser session = getSession();
        return ygOrderSendDetailService.closeSendOrderDetail(id, remark, session.getTrueName());
    }

    @RequestMapping("export")
    public void export(OrderSendDetailSearchParam sendDetailSearchParam, HttpServletRequest request, HttpServletResponse response) {
        sendDetailSearchParam.setEnd(10000);
        List<YgOrderSendDetail> list = ygOrderSendDetailService.findList(sendDetailSearchParam);

        byte[] bytes = fileService.downloadFromOss("template/order_send_detail_export.xlsx");
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("resultSet", list);
        DocumentHandler.DownloadExcel(bytes, "订单发货明细.xlsx", dataMap, request, response);
    }

    @RequestMapping("downloadTemplate")
    public void downloadOrderTemplate(HttpServletRequest request, HttpServletResponse response) {
        byte[] bytes = fileService.downloadFromOss("template/order_send_detail_import.xlsx");
        DocumentHandler.createDownload(bytes, "发货模板.xlsx", request, response);
    }

    @RequestMapping("sendBatch")
    @ResponseBody
    public Message sendBatch(@RequestBody List<OrderDetailSendEntity> sendList) {
        AdminUser session = getSession();
        return ygOrderSendDetailService.sendOrderDetailBatch(sendList, session.getTrueName(), new Date());
    }

    @RequestMapping("loadSendOrderExcel")
    @ResponseBody
    public Message loadSendOrderExcel(@RequestParam MultipartFile uploadFile) {

        return sendOrderDetailImportHandler.loadSendOrderExcel(uploadFile);
    }

    @RequestMapping("splitSendDetails")
    @ResponseBody
    public Message splitSendDetails(Long orderId) {
        int i = splitSendDetailHandler.splitSendDetails(orderId);
        return Message.success(i);
    }

    @RequestMapping("updateNeedSendTime")
    @ResponseBody
    public Message updateNeedSendTime(Long detailId, Date sendTime, String remark) {
        AdminUser session = getSession();
        int i = ygOrderSendDetailService.updateNeedSendTime(detailId, sendTime, session.getTrueName(), remark);
        return Message.success(i);
    }


}
